package com.example.manager.service;

import com.example.common.Ov.AdminOv;
import com.example.common.Ov.UserOv;
import com.example.common.entity.Const;
import com.example.common.entity.Rsp;
import com.example.common.pojo.Admin;
import com.example.common.util.JWTUtil;
import com.example.manager.mapper.AdminMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class AdminService {
    @Autowired
    AdminMapper adminMapper;

    //登录
    public Rsp login(Admin admin) {
        Admin login = adminMapper.login(admin);
        UserOv userOv = new UserOv();

        if (StringUtils.isEmpty(login)) {
            return new Rsp(50000, "登录失败");
        } else {
            userOv.setId(login.getId());
            userOv.setAccount(login.getuserName());
            userOv.setPassword(login.getPassword());
            String token = JWTUtil.generateToken(userOv, Const.PRIVATE_KEY, 30);
            return new Rsp(20000, token, "登录成功");
        }


    }

    //获取信息
    public Rsp getUserInfo(Integer id) {
        AdminOv userInfo = adminMapper.getUserInfo(id);
        if (StringUtils.isEmpty(userInfo)) {
            return new Rsp(500000, "获取失败");
        } else {
            userInfo.setPassword("");
            return new Rsp(20000, userInfo, "获取成功");
        }
    }
}
